<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                  <el-form-item label="单位名称" prop="accountName">
                    <el-input
                        v-model="queryParams.accountName"
                        placeholder="请输入单位名称"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="当前记账年月" prop="accountCurrentAccountingYear">
                    <el-date-picker clearable
                                    v-model="queryParams.accountCurrentAccountingYear"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择当前记账年月">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="账套启用年月" prop="accountAccountOpeningDate">
                    <el-date-picker clearable
                                    v-model="queryParams.accountAccountOpeningDate"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择账套启用年月">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="有效期至" prop="accountValidUntil">
                    <el-date-picker clearable
                                    v-model="queryParams.accountValidUntil"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择有效期至">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="凭证审核" prop="accountCertificateAuditId">
                    <el-select v-model="queryParams.accountCertificateAuditId" placeholder="请选择凭证审核" clearable>
                      <el-option
                          v-for="dict in dict.type.account_certificate_audit_id"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                      />
                    </el-select>
                  </el-form-item>
                  <el-form-item label="创建时间" prop="accountCreatetime">
                    <el-date-picker clearable
                                    v-model="queryParams.accountCreatetime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择创建时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="统一社会信用代码" prop="accountCreditCode">
                    <el-input
                        v-model="queryParams.accountCreditCode"
                        placeholder="请输入统一社会信用代码"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="单位所在地外键id" prop="accountAddressId">
                    <el-input
                        v-model="queryParams.accountAddressId"
                        placeholder="请输入单位所在地外键id"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="行业外键id" prop="accountProfessionId">
                    <el-input
                        v-model="queryParams.accountProfessionId"
                        placeholder="请输入行业外键id"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="租户id" prop="tenantId">
                    <el-input
                        v-model="queryParams.tenantId"
                        placeholder="请输入租户id"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="使用人id" prop="userId">
                    <el-input
                        v-model="queryParams.userId"
                        placeholder="请输入使用人id"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="账套模版外键id" prop="templateId">
                    <el-input
                        v-model="queryParams.templateId"
                        placeholder="请输入账套模版外键id"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['accountSet:accounts:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['accountSet:accounts:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['accountSet:accounts:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['accountSet:accounts:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="accountsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账套id" align="center" prop="id" />
      <el-table-column label="单位名称" align="center" prop="accountName" />
      <el-table-column
        label="会计准则"
        align="center"
        prop="templateAccountingStandards"
      />
      <el-table-column
        label="当前记账年月"
        align="center"
        prop="accountCurrentAccountingYear"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.accountCurrentAccountingYear, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="账套启用年月"
        align="center"
        prop="accountAccountOpeningDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.accountAccountOpeningDate, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="有效期至"
        align="center"
        prop="accountValidUntil"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.accountValidUntil, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="凭证审核"
        align="center"
        prop="accountCertificateAuditId"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.account_certificate_audit_id"
            :value="scope.row.accountCertificateAuditId"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="accountCreatetime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.accountCreatetime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="统一社会信用代码"
        align="center"
        prop="accountCreditCode"
      /> -->
      <!-- <el-table-column
        label="单位所在地外键id"
        align="center"
        prop="accountAddressId"
      /> -->
      <!-- <el-table-column
        label="行业外键id"
        align="center"
        prop="accountProfessionId"
      /> -->
      <!-- <el-table-column label="租户id" align="center" prop="tenantId" /> -->
      <!-- <el-table-column label="使用人id" align="center" prop="userId" /> -->
      <!-- <el-table-column
        label="账套模版外键id"
        align="center"
        prop="templateId"
      /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['accountSet:accounts:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['accountSet:accounts:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改账套对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位名称" prop="accountName">
              <el-input
                v-model="form.accountName"
                placeholder="请输入单位名称"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="账套启用年月" prop="accountAccountOpeningDate">
              <el-date-picker
                clearable
                v-model="form.accountAccountOpeningDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择账套启用年月"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="账套模版" prop="templateId">
          <el-select
            v-model="form.templateId"
            placeholder="请选择账套模版"
            style="width: 650px"
            @change="showRadio(form.templateId)"
          >
            <!-- 这里设置了宽度为 200px，你可以根据实际需求调整数值 -->
            <el-option
              v-for="item in telplageData"
              :key="item.id"
              :label="item.templateAccountingTemplate"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-row>
          <el-col :span="11">
            <el-form-item label="会计准则" prop="templateId">
              <el-select
                v-model="form.templateId"
                placeholder="请选择会计准则"
                disabled
              >
                <!-- 添加 disabled 属性禁用 el-select -->
                <el-option
                  v-for="item in StandardsData"
                  :key="item.id"
                  :label="item.templateAccountingStandards"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="增值税种类" prop="templateId">
              <el-radio
                v-model="TEMform.templateTypesOfVat"
                label="小规模纳税人"
                disabled
                >小规模纳税人</el-radio
              >
              <!-- 给每个 el-radio 添加 disabled 属性 -->
              <el-radio
                v-model="TEMform.templateTypesOfVat"
                label="一般纳税人"
                disabled
                >一般纳税人</el-radio
              >
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="统一社会信用代码" prop="accountCreditCode">
              <el-input
                v-model="form.accountCreditCode"
                placeholder="请输入统一社会信用代码"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="单位所在地">
              <!-- <el-select v-model="form.accountAddressId" placeholder="请选择">
                <el-option
                  v-for="item in addressData"
                  :key="item.id"
                  :label="item.addressName"
                  :value="item.id"
                >
                       
                </el-option>
              </el-select> -->

              <el-form-item class="selform">
                <el-cascader
                  :options="CityInfo"
                  v-model="form.selectedOptions"
                  :change-on-select="true"
                  :clearable="true"
                  :filterable="true"
                  @change="handleChange"
                >
                </el-cascader>
                <div>
                  <!-- <span
                    >所选地区code码：{{ formChina.city }}{{ formChina.erae
                    }}{{ formChina.minerae }}</span -->
                  <!-- > -->
                </div>
              </el-form-item>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="9">
            <el-form-item label="行业" prop="accountProfessionId">
              <el-select
                v-model="form.accountProfessionId"
                placeholder="请选择行业"
              >
                <el-option
                  v-for="item in ProfessionData"
                  :key="item.id"
                  :label="item.professionName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="凭证审核" prop="accountCertificateAuditId">
              <el-radio-group v-model="form.accountCertificateAudauitId">
                <el-radio
                  v-for="dict in dict.type.account_certificate_audit_id"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-form-item label="当前记账年月" prop="accountCurrentAccountingYear">
          <el-date-picker
            clearable
            v-model="form.accountCurrentAccountingYear"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择当前记账年月"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="有效期至" prop="accountValidUntil">
          <el-date-picker
            clearable
            v-model="form.accountValidUntil"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效期至"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="创建时间" prop="accountCreatetime">
          <el-date-picker
            clearable
            v-model="form.accountCreatetime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间"
          >
          </el-date-picker>
        </el-form-item> -->

        <!-- <el-form-item label="租户id" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户id" />
        </el-form-item>
        <el-form-item label="使用人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入使用人id" />
        </el-form-item>
        <el-form-item label="账套模版外键id" prop="templateId">
          <el-input
            v-model="form.templateId"
            placeholder="请输入账套模版外键id"
          />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style scoped>
.form-row {
  display: flex;
  align-items: center; /* 垂直居中对齐内部元素 */
}

.form-row.el-form-item:first-child {
  margin-right: 20px;
}
</style>
<script>
//账套模版api
import {
  listTemplate,
  getTemplate,
  delTemplate,
  addTemplate,
  updateTemplate,
} from "@/api/accountSet/template";
// getlistChinas
import { getlistChinas } from "@/api/accountSet/chinas";
//地址api
import { listAddress } from "@/api/accountSet/address";
//行业api
import { listProfession } from "@/api/accountSet/profession";
import { listChinas } from "@/api/accountSet/chinas";
import {
  listAccounts,
  getAccounts,
  delAccounts,
  addAccounts,
  updateAccounts,
} from "@/api/accountSet/accounts";
import { parseTime } from "@/utils/vctgo";

export default {
  name: "Accounts",
  dicts: ["account_certificate_audit_id"],
  data() {
    return {
      CityInfo: [], //地区数据
      formChina: {
        city: "",
        erae: "",
        minerae: "",
        selectedOptions: [], //地区筛选数组
      },
      //账套模版
      telplageData: [],
      //会计准则
      StandardsData: [],
      //单位所在地
      addressData: [],
      //行业
      ProfessionData: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账套表格数据
      accountsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountName: null,
        accountCurrentAccountingYear: null,
        accountAccountOpeningDate: null,
        accountValidUntil: null,
        accountCertificateAuditId: null,
        accountCreatetime: null,
        accountCreditCode: null,
        accountAddressId: null,
        accountProfessionId: null,
        tenantId: null,
        userId: null,
        templateId: null,
      },
      // 表单参数
      form: {},
      TEMform: {},
      // 表单校验
      rules: {
        accountName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" },
        ],
        accountCurrentAccountingYear: [
          { required: true, message: "当前记账年月不能为空", trigger: "blur" },
        ],
        accountAccountOpeningDate: [
          { required: true, message: "账套启用年月不能为空", trigger: "blur" },
        ],
        accountValidUntil: [
          { required: true, message: "有效期至不能为空", trigger: "blur" },
        ],
        accountCertificateAuditId: [
          { required: true, message: "凭证审核不能为空", trigger: "change" },
        ],
        accountCreatetime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        accountCreditCode: [
          {
            required: true,
            message: "统一社会信用代码不能为空",
            trigger: "blur",
          },
        ],
        accountAddressId: [
          {
            required: true,
            message: "单位所在地外键id不能为空",
            trigger: "blur",
          },
        ],
        accountProfessionId: [
          { required: true, message: "行业外键id不能为空", trigger: "blur" },
        ],
        tenantId: [
          { required: true, message: "租户id不能为空", trigger: "blur" },
        ],
        userId: [
          { required: true, message: "使用人id不能为空", trigger: "blur" },
        ],
        templateId: [
          {
            required: true,
            message: "账套模版外键id不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },

  created() {
    this.getList();
    this.getTemplateList();
    this.getAddressList();
    this.getProfessionList();
    this.getRegions();
  },
  methods: {
    handleChange(value) {
      this.formChina.city = this.formChina.selectedOptions[0];
      this.formChina.erae = this.formChina.selectedOptions[1];
      this.formChina.minerae = this.formChina.selectedOptions[2];
    },
    getRegions() {
      // getlistChinas().then((response) => {
      //   this.CityInfo = response.data;
      // });

      // debugger;
      var that = this;
      getlistChinas().then((res) => {
        console.log(res.data);
        const jsona = JSON.stringify(res.data); // 把接口返回的res.data数据转成字符串
        const jsonb = jsona.replace(/"code"/g, '"value"'); // 修改成你要的字段
        const jsonc = jsonb.replace(/"name"/g, '"label"');
        const endjson = JSON.parse(jsonc); //  把JSON 字符串转换成对象\
        console.log(endjson);
        that.CityInfo = endjson;
      });
    },

    /** 查询账套列表 */
    getList() {
      this.loading = true;
      listAccounts(this.queryParams).then((response) => {
        this.accountsList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 回显单选框
    showRadio(id) {
      // console.log(id);
      getTemplate(id).then((response) => {
        // TEMform.templateTypesOfVat = response.data.templateTypesOfVat;
        this.TEMform = response.data;
        console.log(response.data);
        // this.form = response.data;
        // this.open = true;
        // this.title = "修改账套模版";
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        accountName: null,
        accountCurrentAccountingYear: null,
        accountAccountOpeningDate: null,
        accountValidUntil: null,
        accountCertificateAuditId: 0,
        accountCreatetime: null,
        accountCreditCode: null,
        accountAddressId: null,
        accountProfessionId: null,
        selectedOptions: [],
        tenantId: null,
        userId: null,
        templateId: null,
      };
      this.TEMform = {
        templateAccountingTemplate: null,
        templateAccountingStandards: null,
        templateTypesOfVat: null,
      };
      this.resetForm("form");
    },

    getTemplateList() {
      listTemplate().then((response) => {
        this.telplageData = response.data;
        this.StandardsData = response.data;
        this.getList();
      });
    },
    getAddressList() {
      listAddress().then((response) => {
        this.addressData = response.data;
        this.getList();
      });
    },

    getProfessionList() {
      listProfession().then((response) => {
        this.ProfessionData = response.data;
        this.getList();
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加账套";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAccounts(id).then((response) => {
        this.form = response.data;
      });

      getTemplate(id).then((response) => {
        this.TEMform = response.data;
      });

      this.open = true;
      this.title = "修改账套";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAccounts(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccounts(this.form).then((response) => {
              console.log(form);
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除当前账套数据项？")
        .then(function () {
          return delAccounts(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "accountSet/accounts/export",
        {
          ...this.queryParams,
        },
        `账套_${parseTime(new Date().getTime(), "{y}-{m}-{d}")}.xlsx`
      );
    },
  },
};
</script>